*** Posterior Probability Analysis

	*** Prepare final posterior probabilities
		use "$covidclean/smscovid_clean.dta", clear
		keep if behavior_treatment == 1
		keep if consent == 1 & age >= 18
		keep if threedayrecall == 1
		gen distancingoutcome = act_sd
		rename distancingoutcome outcome
		keeporder outcome treatment_arm
		drop if missing(outcome)
		gen treatment = treatment_arm
		drop treatment_arm 
		preserve 
			collapse (count) outcome, by(treatment)
			rename outcome count 
			tempfile sdcount
			save `sdcount'
		restore 
		export delimited "$output/Adaptive_Trial/finaldistancingpriors.csv", replace
		
		use "$covidclean/smscovid_clean.dta", clear
		keep if behavior_treatment == 2
		keep if consent == 1 & age >= 18
		keep if threedayrecall == 1
		gen handwashingoutcome = 1 if act_hwonly == 1 | act_hwsoap == 1
		replace handwashingoutcome = 0 if missing(handwashingoutcome) & !missing(act_hwonly)
		rename handwashingoutcome outcome
		keeporder outcome treatment_arm
		drop if missing(outcome)
		gen treatment = treatment_arm
		drop treatment_arm 
		preserve 
			collapse (count) outcome, by(treatment)
			rename outcome count 
			tempfile hwcount
			save `hwcount'
		restore 
		export delimited "$output/Adaptive_Trial/finalhwpriors.csv", replace
		
	// Run R-code here
	
	*** Analyze 
		import delimited "$root/DATA/Adaptive_Trial_Output/Posteriors/final_bayes_table_hw.csv", clear
		label define arms 1 "Neutral - twice morning" 2 "Public gain - twice morning" 3 "Public loss - twice morning" 4 "Private gain - twice morning" 5 "Private loss - twice morning" 6 "Neutral - morning/evening" 7 "Public gain - morning/evening" 8 "Public loss - morning/evening" 9 "Private gain - morning/evening" 10 "Private loss - morning/evening"
		label values treatment arms 
		decode treatment, gen(arm)
		merge 1:1 treatment using `hwcount'
		drop treatment	_merge 
		gen behavior = "Handwashing"
		order behavior arm
		tempfile handwashing 
		save `handwashing'
		
		import delimited "$root/DATA/Adaptive_Trial_Output/Posteriors/final_bayes_table_distancing.csv", clear
		label define arms 1 "Neutral - twice morning" 2 "Public gain - twice morning" 3 "Public loss - twice morning" 4 "Private gain - twice morning" 5 "Private loss - twice morning" 6 "Neutral - morning/evening" 7 "Public gain - morning/evening" 8 "Public loss - morning/evening" 9 "Private gain - morning/evening" 10 "Private loss - morning/evening"
		label values treatment arms 
		decode treatment, gen(arm)
		merge 1:1 treatment using `sdcount'
		drop treatment	_merge 	
		gen behavior = "Distancing"
		append using `handwashing'
		gen timing = "Morn./Even." if regexm(arm, "morning/evening")==1
		replace timing = "2\(\times\)morning" if regexm(arm, "twice morning")==1
		replace arm = subinstr(arm, " - twice morning", "",1)
		replace arm = subinstr(arm, " - morning/evening", "",1)
		label var behavior "Behavior"
		label var arm "Framing"
		label var timing "Timing"
		label var mean "$\mu_j$"
		label var standarddev "$\sigma_j$"
		label var probabilityoptimal "\(p^j\)"
		label var count "N"
		order behavior arm timing count
		format count %9.0fc
		format mean %9.3fc
		format standarddev %9.3fc
		format probabilityoptimal %9.3fc
		compress
		replace arm = "\text{" + arm + "}"
		texsave using "$tables/posteriorprobtable.tex", varlabels replace frag nofix
